<?php

namespace App\Repository;

//use App\Entity\CommonLog;
//use App\Entity\Miner;
use App\Entity\User;
//use App\Entity\UserGoldBrickProfits;
//use App\Entity\UserMiner;
//use App\Entity\UserProfits;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
//use Symfony\Component\Serializer\Serializer;

/**
 * @method User|null find($id, $lockMode = null, $lockVersion = null)
 * @method User|null findOneBy(array $criteria, array $orderBy = null)
 * @method User[]    findAll()
 * @method User[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
{
    public $userLevelRepository;

    public function __construct(
        ManagerRegistry $registry
        ,UserLevelRepository $userLevelRepository
    )
    {
        parent::__construct($registry, User::class);
        $this->userLevelRepository = $userLevelRepository;
    }

    /**
     * Used to upgrade (rehash) the user's password automatically over time.
     */
    public function upgradePassword(UserInterface $user, string $newEncodedPassword): void
    {
        if (!$user instanceof User) {
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', \get_class($user)));
        }

        $user->setPassword($newEncodedPassword);
        $this->_em->persist($user);
        $this->_em->flush();
    }

    /**
     * @param $value
     * @return User|null
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
    public function findOneByPhone($value): ?User
    {
        return $this->createQueryBuilder('u')
            ->andWhere('u.phone = :val')
            ->setParameter('val', $value)
            ->getQuery()
            ->getOneOrNullResult();
    }

    public function dealWithLevel(User $user)
    {
        $list = $this->userLevelRepository->findBy(array(),array("sort"=>"ASC"));
        $lastExpr = 0;
        foreach ($list as $key => $row) {
            if ($user->getExpr() > $lastExpr && $user->getExpr() <= $row->getExpr()) {
                $user->setLevel($row);
            }
            if ($user->getExpr() >  $row->getExpr() && $key == count($list) - 1 ) {
                $user->setLevel($row);
            }
        }
        $this->_em->persist($user);
        $this->_em->flush();
    }

//    /**
//     * 用户注册赠送矿机
//     * @param User $user
//     * @return UserMiner|bool
//     */
//    public function addMiner4Reg(User $user)
//    {
//        $miners = $this->minerRepository->findBy(array(), array("sort" => "ASC"), 1, 0);
//        $miner = $miners[0] ?? null;
//        $userMiner = $this->userMinerRepository->findOneBy(array("user" => $user, "miner" => $miner));
//        if ($userMiner) {
//            //已经赠送过了
//            return false;
//        } else {
//            $UserMiner = new UserMiner();
//        }
//        $UserMiner->setUser($user)
//            ->setMiner($miner)
//            ->setExpireAt(new \DateTime("+ 1 month"));
//        $this->_em->persist($UserMiner);
//        $this->_em->flush();
//        return $UserMiner;
//    }
//
//    /**
//     * 检查用户是否被禁止
//     * @param User $user
//     * @return bool|string
//     */
//    public function checkBan(User $user)
//    {
//        $list = $user->getPunishLogs();
//        $now = new \DateTime();
//        $max = new \DateTime();
//        foreach ($list as $row) {
//            if ($row->getExpiredAt() > $max) {
//                $max = $row->getExpiredAt();
//            }
//        }
//        if ($max > $now) {
//            return $max->format(YYMMDDHHIISS);
//        } else {
//            return false;
//        }
//    }

}
